package com.loong.system.controller.forehead;

import com.loong.common.mvc.BaseForeheadController;
import com.loong.common.param.BaseIdParam;
import com.loong.common.rest.Result;
import com.loong.common.validator.group.CommonValidate;
import com.loong.global.dto.UserDTO;
import com.loong.system.common.forehead.param.DeleteSystemLetterForeheadParam;
import com.loong.system.common.forehead.param.SystemLetterPageQueryForeheadParam;
import com.loong.system.handler.SystemLetterHandler;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @Description: 处理c端站内信相关请求的controller
 * @Author: LiuQun
 * @Date: 2021/1/2 14:27
 */
@RestController
@RequestMapping("forehead/system/letter")
public class SystemLetterForeheadController extends BaseForeheadController {

    @Resource
    private SystemLetterHandler letterHandler;

    /**
     * 新增系统发送的站内信
     * @return 响应的结果
     */
    @PostMapping("save/systemLetter")
    public Result saveSystemLetter(){
        UserDTO userDTO = super.getUserDTO();
        return handlerExecutor.execute(() -> letterHandler.foreheadSaveSystemLetter(userDTO));
    }

    /**
     * 批量删除自己拉取的站内信
     * @param param 站内信删除封装的参数
     * @return 响应的结果
     */
    @PostMapping("batchDelete")
    public Result batchDeleteLetter(@RequestBody @Validated({CommonValidate.class})DeleteSystemLetterForeheadParam param){
        param.setUserId(super.getUserDTO().getId());
        return handlerExecutor.execute(() -> letterHandler.foreheadBatchDeleteLetters(param));
    }

    /**
     * 更新回复反馈类型的站内信的接收时间
     * @return 响应的结果
     */
    @PostMapping("update/receiveTime")
    public Result updateRepliedLetterReceiveTime(){
        Long userId = super.getUserDTO().getId();
        return handlerExecutor.execute(() -> letterHandler.foreheadUpdateRepliedLetterReceiveTime(userId));
    }

    /**
     * 更新站内信的已读状态
     * @param param 站内信id
     * @return 响应的结果
     */
    @PostMapping("update/readStatus")
    public Result updateLetterReadStatus(@RequestBody @Validated({CommonValidate.class})BaseIdParam param){
        return handlerExecutor.execute(() -> letterHandler.foreheadUpdateLetterReadStatus(param.getId()));
    }

    /**
     * 获取未读的站内信数量
     * @return 响应的结果
     */
    @PostMapping("unReadNum")
    public Result getUnReadLetterNum(){
        Long userId = super.getUserDTO().getId();
        return handlerExecutor.execute(() -> letterHandler.foreheadGetUnReadLetterNum(userId));
    }

    /**
     * 分页查询自己的站内信
     * @param param 查询参数
     * @return 响应的结果
     */
    @PostMapping("pageQuery/LetterList")
    public Result pageQueryLetter(@RequestBody SystemLetterPageQueryForeheadParam param){
        param.setUserId(super.getUserDTO().getId());
        return handlerExecutor.execute(() -> letterHandler.foreheadPageQueryLetter(param));
    }

    /**
     * 根据id查询站内信详情
     * @param param 站内信id
     * @return 响应的结果
     */
    @PostMapping("find/detail")
    public Result findLetterDetailById(@RequestBody @Validated({CommonValidate.class})BaseIdParam param){
        return handlerExecutor.execute(() -> letterHandler.foreheadFindLetterDetailById(param.getId()));
    }
}
